package man

import (
	"dub/define"
	"dub/define/grpcmodel"
	"dub/secrec"
	"regexp"
)

type AgentManController struct {
	AgentManBaseController
}

func (a *AgentManController) Get() {
	a.TplName = "man/agentMan.html"
}
func (a *AgentManController) FindListAgent() {
	//page,rows
	page, _ := a.GetUint32("page", 1)
	pageSize, _ := a.GetUint32("rows", 20)
	// rows := make([]grpcmodel.ModelSecRpcAgent, 0)
	rows := make([]*grpcmodel.ModelSecRpcAgent, 0)
	jsons := make(map[string]interface{})
	jsons["total"] = 0
	jsons["rows"] = rows

	//得到登录id
	int_man := a.GetSession(define.Web_Session_Name_Man_Login_Use)
	man_val, man_ok := int_man.(grpcmodel.ModelSecRpcUseResLogin)
	if !man_ok {
		a.Data["msg"] = "参数不合法"
		return
	}

	args := &grpcmodel.ModelSecRpcAgentReqFind{
		IsPage:   true,
		StartRow: uint32((page - 1) * pageSize),
		PageSize: uint32(pageSize),
		AddUseId: man_val.GetModelSecRpcUse().GetUserId(),
	}
	use_rpc := secrec.GetRpc(secrec.ConstServiceUseRpc)
	if use_rpc == nil {
		a.Log().Errorf("agent_man_controller.go FindListAgent method not find use_rpc info GetRpc\n")
		a.ServeJSON()
		return
	}
	sec_agent := grpcmodel.NewISecRpcAgentClient(use_rpc.Conn())
	reply, err := sec_agent.Find(use_rpc.Context(), args)
	if err != nil {
		a.Log().Errorf("agent_man_controller.go FindListAgent method s.dbRpc.Call SecAgentRpc.FindAgentsByConds err. %v\n", err)
		a.Data["json"] = jsons
		a.ServeJSON()
		return
	}

	if reply.Err > 0 {
		a.Data["json"] = jsons
		a.ServeJSON()
		return
	}

	// for _, rep_row := range reply.GetRows() {
	// 	rows = append(rows, *rep_row)
	// }
	rows = reply.GetRows()
	jsons["total"] = reply.Total
	jsons["rows"] = rows
	a.Data["json"] = jsons
	a.ServeJSON()
}

func (a *AgentManController) ToAddAgent() {
	a.TplName = "man/toAddAgent.html"
}

func (a *AgentManController) AddAgent() {
	a.Data["result"] = "false"
	a.Data["msg"] = ""

	loginName := a.GetString("name", "")
	loginNameVail, _ := regexp.MatchString("^[0-9a-zA-Z]{5,}", loginName)
	if !loginNameVail {
		a.Data["msg"] = "参数不合法"
		return
	}

	loginPwd := a.GetString("pwd", "")
	loginPwdVail, _ := regexp.MatchString("^.{6,12}", loginPwd)
	if !loginPwdVail {
		a.Data["msg"] = "参数不合法"
		return
	}

	recode := a.GetString("recode3", "")
	recodeVail, _ := regexp.MatchString("^[0-9]{6}", recode)
	if !recodeVail {
		a.Data["msg"] = "参数不合法"
		return
	}
	price, _ := a.GetFloat("price", 0.00)
	//得到登录id
	int_man := a.GetSession(define.Web_Session_Name_Man_Login_Use)
	man_val, man_ok := int_man.(grpcmodel.ModelSecRpcUseResLogin)
	if !man_ok {
		a.Data["msg"] = "参数不合法"
		return
	}

	use_rpc := secrec.GetRpc(secrec.ConstServiceUseRpc)
	if use_rpc == nil {
		a.Log().Errorf("agent_man_controller.go AddAgent method not find use_rpc info GetRpc\n")
		return
	}

	args := &grpcmodel.ModelSecRpcAgentReqAdd{
		ModelSecRpcAgent: &grpcmodel.ModelSecRpcAgent{
			RegionCode: recode,
			LoginName:  loginName,
			LoginPwd:   loginPwd,
			AgentPrice: price,
			AddUseId:   man_val.GetModelSecRpcUse().GetUserId(),
		},
	}
	sec_agent := grpcmodel.NewISecRpcAgentClient(use_rpc.Conn())
	reply, err := sec_agent.Add(use_rpc.Context(), args)
	if err != nil {
		a.Data["msg"] = "没有此权限"
		a.Log().Errorf("agent_man_controller.go AddAgent method s.dbRpc.Call SecAgentRpc.AddAgent err. %v\n", err)
		return
	}

	if reply.GetErr() > 1 {
		a.Data["msg"] = "没有此权限"
		return
	} else if reply.GetErr() == 1 {
		a.Data["msg"] = "登录名重复"
		return
	}
	a.TplName = "man/agentMan.html"
}
